<!-- 订单标签组件 -->
<template>
  <div class="order-tags">
    <div class="order-tags-header">
      <el-button type="primary" size="small" @click="handleAdd">新增标签</el-button>
      <el-button type="danger" size="small" @click="handleDelete" :disabled="selectedIds.length === 0">批量删除</el-button>
    </div>

    <el-table
      ref="tagsTable"
      :data="tagsList"
      stripe
      border
      size="small"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="tagName" label="标签名称" align="center" width="150" />
      <el-table-column prop="tagCode" label="标签代码" align="center" width="150" />
      <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeleteSingle(scope.row)"
            style="color: #f56c6c;"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增标签弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="form.tagName" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item label="标签代码" prop="tagCode">
          <el-input v-model="form.tagCode" placeholder="请输入标签代码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'OrderTags',
  props: {
    cellId: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      tagsList: [],
      selectedIds: [],
      open: false,
      title: '',
      form: {
        tagName: '',
        tagCode: '',
        remark: ''
      },
      rules: {
        tagName: [
          { required: true, message: '标签名称不能为空', trigger: 'blur' }
        ],
        tagCode: [
          { required: true, message: '标签代码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    cellId: {
      handler(newVal) {
        if (newVal) {
          this.getTagsList()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取标签列表
    getTagsList() {
      if (!this.cellId) return

      this.loading = true
      // 模拟接口调用
      setTimeout(() => {
        this.tagsList = [
          {
            id: 1,
            tagName: '重要订单',
            tagCode: 'IMPORTANT',
            remark: '需要优先处理的重要订单'
          },
          {
            id: 2,
            tagName: '快速发货',
            tagCode: 'FAST_SHIP',
            remark: '需要快速发货的订单'
          }
        ]
        this.loading = false
      }, 500)

      // TODO: 替换为实际API调用
      // getOrderTags(this.cellId).then(response => {
      //   this.tagsList = response.data
      //   this.loading = false
      // }).catch(() => {
      //   this.loading = false
      // })
    },

    // 新增标签
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增标签'
    },

    // 表格选择变化
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.id)
    },

    // 批量删除
    handleDelete() {
      this.$confirm('确认删除选中的标签吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除接口
        this.$message.success('删除成功')
        this.getTagsList()
      })
    },

    // 单个删除
    handleDeleteSingle(row) {
      this.$confirm('确认删除该标签吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除接口
        this.$message.success('删除成功')
        this.getTagsList()
      })
    },

    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // TODO: 调用新增接口
          this.$message.success('新增成功')
          this.open = false
          this.getTagsList()
        }
      })
    },

    // 取消
    cancel() {
      this.open = false
      this.reset()
    },

    // 重置表单
    reset() {
      this.form = {
        tagName: '',
        tagCode: '',
        remark: ''
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate()
      }
    }
  }
}
</script>

<style scoped>
.order-tags {
  padding: 10px;
}

.order-tags-header {
  margin-bottom: 10px;
}

.order-tags-header .el-button {
  margin-right: 10px;
}
</style>
